﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
using EntityFramework.Extensions;
using System.Data;
using System.Data.SqlClient;
using DevExpress.Web.Mvc;
using DevExpress.Web.ASPxGridView;
using DevExpress.Utils;
using System.Web.UI.WebControls;
using System.Collections;
namespace Webcar.Controllers
{
    public partial class TableController : BaseControler
    {
        public ActionResult LeaderCarList()
        {
            return View("LeaderCarList");
        }
        public ActionResult LeaderCarExport()
        {
            GridViewSettings settings = new GridViewSettings();
            settings.Name = "gridView";
            DevControlHelper.InitDefaultPagerGridViewSettings(settings);
            settings.SettingsPager.Mode = GridViewPagerMode.ShowAllRecords;
            settings.EnableAlternatingRowColor();
            settings.EnableRowIndexColumn();
            var deptcol = settings.Columns.Add("deptname", "单位");
            deptcol.CellStyle.Wrap = DefaultBoolean.False;
            deptcol.Width = Unit.Pixel(200);
            deptcol.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("CAR_RECORD", "车辆牌照").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.Columns.Add("IS_GPS", "是否安装GPS").HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            settings.AddLeaderCarStatusColumn("STATUS", "使用情况");
            settings.Columns.Add("NOTE", "说明事项").HeaderStyle.HorizontalAlign = HorizontalAlign.Left;

            var data = GetData().AsQueryable().ToDataTable();
            return GridViewExtension.ExportToXls(settings, data, "车辆GPS安装信息信息");
        }

        public IEnumerable GetData()
        {
            var list = from p in CurrentUser.UserGrantedDept.Where(a => a.super_dept == null && a.use_flag == "T")
                       join p2 in WebcarDataProvider.DB.bs_leader_cars
                       on p.deptcode equals p2.DEPT_CODE
                       select new
                       {
                           ID = p2.ID,
                           dept_code = p.deptcode,
                           deptname = p.deptname,
                           NAME = p2.NAME,
                           JOB = p2.JOB,
                           CAR_RECORD = p2.CAR_RECORD,
                           IS_GPS = p2.IS_GPS ? "是" : "否",
                           STATUS = p2.STATUS,
                           ADDRESS = p2.ADDRESS,
                           NOTE = p2.NOTE
                       };

            return list.ToArray();
        }
        public ActionResult LeaderCarListPartialView()
        {
            var list = GetData();
            return PartialView(model: list);
        }
        public ActionResult LeaderCarDelete(int? id)
        {
            if (id == null || id.HasValue == false) return LeaderCarList();
            using (var DB = new WebcarContext())
            {
                bs_leader_cars group = DB.bs_leader_cars.Find(id);
                DB.bs_leader_cars.Remove(group);
                DB.SaveChanges();
            }
            return LeaderCarList();
        }
        public ActionResult LeaderCarEdit(int? id)
        {
            bs_leader_cars model = null;
            if (id.HasValue)
            {
                model = WebcarDataProvider.DB.bs_leader_cars.Find(id);
            }
            if (model == null)
            {
                model = new bs_leader_cars();
            }
            //var dept = WebcarDataProvider.DB.sys_dept.Find(id);
            //if (dept != null)
            //{
            //    ViewBag.DeptName = dept.deptname;
            //}
            return PartialView(model: model);
        }
        public ActionResult LeaderCarEditSave(bs_leader_cars model)
        {
            string error = "";
            if (CheckLeaderCarModel(model, ref error) == false)
            {
                return Content(error);
            }
            using (var DB = new WebcarContext())
            {
                bs_leader_cars item = DB.bs_leader_cars.Find(model.ID);
                if (item == null)
                {
                    DB.bs_leader_cars.Add(model);
                }
                else
                {
                    model.CopyPropertyTo(item);
                }
                DB.SaveChanges();
            }
            return SuccessReload();
        }

        bool CheckLeaderCarModel(bs_leader_cars model, ref string error)
        {
            if (model.DEPT_CODE.IsNull())
            {
                error = "请输入单位。";
                return false;
            }
            if (model.CAR_RECORD.IsNull())
            {
                error = "请输入牌照号码。";
                return false;
            }
            //if (model.cjdw_lxr.IsNull())
            //{
            //    error = "请输入承建单位联系人。";
            //    return false;
            //}

            return true;
        }
    }
}